﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GACFFP.Services
{
    public class PeriodValidator
    {

        public PeriodValidator()
        { }

        public static DateTime GetThisWeekStart()
        {
            try
            {
                DateTime today = DateTime.Now;

                int daysToSubtract;

                //Compare day to sunday
                daysToSubtract = ((int)DayOfWeek.Sunday - (int)today.DayOfWeek) % 7;

                //return sunday
                DateTime weekStart = today.AddDays(daysToSubtract);

                return weekStart;
            }
            catch (Exception e)
            { 
                //TODO logging logic
                throw (e);
            }

        }

        public static DateTime GetThisWeekEnd()
        {
            try
            {
                DateTime today = DateTime.Now;

                int daysToSubtract;

                //Compare day to sunday
                daysToSubtract = ((int)DayOfWeek.Saturday - (int)today.DayOfWeek) % 7;
                //set sunday as day

                //return sunday
                DateTime weekEnd = today.AddDays(daysToSubtract);

                return weekEnd;
            }
            catch (Exception e)
            { 
                //TODO Logging logic
                throw(e);
            }
        }


        //Check for within full season
        public static bool IsTodayInSeason()
        {
            try
            {
                GACFFP.Models.GACFFP_Repository gr = new Models.GACFFP_Repository();
                DateTime seasonStart = gr.GetStartOfSeason();
                DateTime seasonEnd = gr.GetEndOfSeason();

                int startOK = DateTime.Compare(DateTime.Now, seasonStart);//should be GREATER than 0
                int endOK = DateTime.Compare(DateTime.Now, seasonEnd);//should be LESS than 0


                if (startOK > 0 && endOK < 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            { 
                //TODO logging logic
                return false;
            }

        }

        //check for within valid voting days of the week.
        public static bool IsTodayValid()
        { 
            GACFFP.Models.GACFFP_Repository gr = new Models.GACFFP_Repository();
            string[] daysOpen = gr.GetAllowedDaysToVote();

            DateTime today = DateTime.Now;

            foreach(var d in daysOpen)
            {
                if(d.Trim()==today.DayOfWeek.ToString())
                {
                    return true;
                }
            }

            return false;
        }
    }
}